<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    /**
    * Main class for working with site pages named "posts".
    * @author NU SIT Bar
    * @version 1.0
    * @created 17.Jul.2012 17:39:44 h
    */

    class Cmssettings extends MX_Controller 
    {

        private $data;

        function __construct()
        {

            parent::__construct();
            if(!$this->session->userdata('logged_in')){
                redirect(base_url());    
            }
            $this->load->config('pages'); 

        }

        public function index()
        { 
            if(!isset($_GET['post_type']))
            {
                $_GET['post_type']="page";
            }
            $this->view_all();
        }

        /**
        * 
        * @param $_GET['post_type']
        */
        function view_all_words()
        {   
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('cmssettings/view_all_words',$this->data);
        }

        function view_new_word()
        {  
            $this->data['post_action'] = "NEW"; 
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'Add '.$_GET['post_type'];
            $this->template->build('cmssettings/view_new_word', $this->data);        
        }

        /**
        * 
        * @param $_GET['post_ID']
        * @param $_GET['post_type']
        */
        function view_edit_word()
        {      
            $this->data['post_action'] = "EDIT";          
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'Edit'.$_GET['post_type'];
            $this->template->build('cmssettings/view_edit_word', $this->data);
        }

        /**
        * 
        * @param $_POST['parent_id']
        * @param $_POST['post_type']
        * @param $_POST['post_title']
        * @param $_POST['post_action']
        * @param $_POST['content']
        * @param $_POST['qtrans_textarea_content']
        */
        function submit_post(){

            //print_r($_POST);
            
            /** module run settings submit_post **/
            switch($_POST['post_type']){

                case 'thumbnails':     

                    $result = modules::run('settings/submit_post_thumbnails');                

                    break;

                case 'words': 

                    $result = modules::run('settings/submit_post_word');

                    break;

            }   

            $result = explode('-',$result);

            // Return
            if($result[0]){
                echo json_encode(array('action'=>true,'msg'=>$result[1]));
            }else{
                echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
            }   

        }

        function delete_word($ID){

            /** module run page submit_post **/
            $result = modules::run('settings/delete_word',array('ID'=>$ID));

            if($result){
                redirect(base_url().'cmssettings/view_all_words?post_type=words');
            }else{
                show_error('Error ocured while deleting word!');
            }

        }

        function view_all_thimbnail_size(){

            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('cmssettings/view_all_thumbnail_size',$this->data);

        }

        function view_edit_thimbnail_size(){

            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('cmssettings/view_edit_thumbnail_size',$this->data);

        }

        function view_add_thimbnail_size(){

            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('cmssettings/view_add_thumbnail_size',$this->data);

        }

        function create_thumbnails_again(){

            $result = modules::run('gallery/create_thumbnails_again'); 
            $result = explode('-',$result);

            // Return
            if($result[0]){
                redirect(base_url('cmssettings/view_all_thimbnail_size?post_type=thumbnails'));
            }else{
                echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
            }    

        }

        function delete_thumbnail(){

            $result = modules::run('settings/delete_thumbnail'); 

            // Return
            if($result){
                redirect(base_url('cmssettings/view_all_thimbnail_size?post_type=thumbnails'));
            }else{
                echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
            }

        }

    }

    /* End of file cmssettings.php */
    /* Location: ./application/modules/pages/controllers/cmssettings.php */